<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电子病历ID" prop="emrId">
        <el-input
          v-model="queryParams.emrId"
          placeholder="请输入电子病历ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊日期" prop="treatmentDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.treatmentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择就诊日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="病种中文名" prop="icdName">
        <el-input
          v-model="queryParams.icdName"
          placeholder="请输入病种中文名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入医生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生职称" prop="doctorTitle">
        <el-input
          v-model="queryParams.doctorTitle"
          placeholder="请输入医生职称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_show_hide"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="诊断结果一致率" prop="isTop">
        <el-select v-model="queryParams.isTop" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_whether"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为无依据诊断" prop="isGroundless">
        <el-select v-model="queryParams.isGroundless" placeholder="请选择是否为无依据诊断 ：0-不是无依据诊断    1-是无依据诊断" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_whether"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小布返回top5数组" prop="xiaobuResult">
        <el-input
          v-model="queryParams.xiaobuResult"
          placeholder="请输入小布返回top5数组"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:ods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:ods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:ods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="odsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="电子病历ID" align="center" prop="emrId" />
      <el-table-column label="就诊科室" align="center" prop="treatmentDepartment" />
      <el-table-column label="就诊日期" align="center" prop="treatmentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.treatmentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="病种中文名" align="center" prop="icdName" />
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="医生职称" align="center" prop="doctorTitle" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_show_hide" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="是否存在高危病种" align="center" prop="isHightDisease">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_job_status" :value="scope.row.isHightDisease"/>
        </template>
      </el-table-column>
      <el-table-column label="诊断结果一致率" align="center" prop="isTop">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_whether" :value="scope.row.isTop"/>
        </template>
      </el-table-column>
      <el-table-column label="是否为无依据诊断" align="center" prop="isGroundless">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_whether" :value="scope.row.isGroundless"/>
        </template>
      </el-table-column>
      <el-table-column label="小布返回top5数组" align="center" prop="xiaobuResult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:ods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改emr原始数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电子病历ID" prop="emrId">
          <el-input v-model="form.emrId" placeholder="请输入电子病历ID" />
        </el-form-item>
        <el-form-item label="科室编码" prop="deptNumber">
          <el-input v-model="form.deptNumber" placeholder="请输入科室编码" />
        </el-form-item>
        <el-form-item label="就诊科室" prop="treatmentDepartment">
          <el-input v-model="form.treatmentDepartment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="就诊日期" prop="treatmentDate">
          <el-date-picker clearable size="small"
            v-model="form.treatmentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择就诊日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="主诉" prop="chiefComplaint">
          <el-input v-model="form.chiefComplaint" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="现病史" prop="hsPresentIllness">
          <el-input v-model="form.hsPresentIllness" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="体格检查" prop="physicalExamination">
          <el-input v-model="form.physicalExamination" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="初步诊断" prop="preliminaryDiagnosis">
          <el-input v-model="form.preliminaryDiagnosis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="诊断ICD编码" prop="icdId">
          <el-input v-model="form.icdId" placeholder="请输入诊断ICD编码" />
        </el-form-item>
        <el-form-item label="病种中文名" prop="icdName">
          <el-input v-model="form.icdName" placeholder="请输入病种中文名" />
        </el-form-item>
        <el-form-item label="医生姓名" prop="doctorName">
          <el-input v-model="form.doctorName" placeholder="请输入医生姓名" />
        </el-form-item>
        <el-form-item label="医生工号" prop="doctorNumber">
          <el-input v-model="form.doctorNumber" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="医生职称" prop="doctorTitle">
          <el-input v-model="form.doctorTitle" placeholder="请输入医生职称" />
        </el-form-item>
        <el-form-item label="专科病历名称" prop="specialRecord">
          <el-input v-model="form.specialRecord" placeholder="请输入专科病历名称" />
        </el-form-item>
        <el-form-item label="状态：0-未处理 1-已处理 2-处理中" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态：0-未处理 1-已处理 2-处理中">
            <el-option
              v-for="dict in dict.type.sys_show_hide"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否存在高危病种">
          <el-checkbox-group v-model="form.isHightDisease">
            <el-checkbox
              v-for="dict in dict.type.sys_job_status"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="诊断结果一致率">
          <el-radio-group v-model="form.isTop">
            <el-radio
              v-for="dict in dict.type.sys_whether"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否为无依据诊断">
          <el-radio-group v-model="form.isGroundless">
            <el-radio
              v-for="dict in dict.type.sys_whether"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="小布返回top5数组" prop="xiaobuResult">
          <el-input v-model="form.xiaobuResult" placeholder="请输入小布返回top5数组" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOds, getOds, delOds, addOds, updateOds, exportOds } from "@/api/system/ods";

export default {
  name: "Ods",
  dicts: ['sys_user_sex', 'sys_show_hide', 'sys_whether'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // emr原始数据表格数据
      odsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        emrId: null,
        treatmentDepartment: null,
        treatmentDate: null,
        sex: null,
        icdName: null,
        doctorName: null,
        doctorTitle: null,
        state: null,
        isHightDisease: null,
        isTop: null,
        isGroundless: null,
        xiaobuResult: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询emr原始数据列表 */
    getList() {
      this.loading = true;
      listOds(this.queryParams).then(response => {
        this.odsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        emrId: null,
        deptNumber: null,
        treatmentDepartment: null,
        treatmentDate: null,
        sex: null,
        age: null,
        height: null,
        weight: null,
        chiefComplaint: null,
        hsPresentIllness: null,
        physicalExamination: null,
        preliminaryDiagnosis: null,
        icdId: null,
        icdName: null,
        doctorName: null,
        doctorNumber: null,
        doctorTitle: null,
        specialRecord: null,
        state: null,
        isHightDisease: [],
        isTop: "0",
        isGroundless: "0",
        xiaobuResult: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加emr原始数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOds(id).then(response => {
        this.form = response.data;
        this.form.isHightDisease = this.form.isHightDisease.split(",");
        this.open = true;
        this.title = "修改emr原始数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.isHightDisease = this.form.isHightDisease.join(",");
          if (this.form.id != null) {
            updateOds(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOds(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除emr原始数据编号为"' + ids + '"的数据项？').then(function() {
        return delOds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有emr原始数据数据项？').then(() => {
        this.exportLoading = true;
        return exportOds(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
